<template>
	<view class="box" style="display: flex; justify-content: center; align-items: center;">
		<view class="shell"
			style="display: flex; flex-direction: column; justify-content: center; align-items: center; opacity: 0.7;
		height: 50%;width: 80%;  border-radius: 25rpx; background-image: linear-gradient(to top, rgb(118, 76, 163), rgb(92, 103, 211));">
			<view class="account "
				style="display: flex; justify-content: space-around; align-items: center; margin:20rpx 0 ;">
				<img src="/static/account.png" alt="" style="height: 50rpx; width: 50rpx;" />
				<u--input v-model="account" placeholder="账号" border="surround" color="white"></u--input>
			</view>
			<view class="password"
				style="display: flex; justify-content: space-around; align-items: center; margin:20rpx 0 ;">
				<img src="/static/password.png" alt="" style="height: 50rpx; width: 50rpx;" />
				<u--input v-model="password" placeholder="密码" border="surround" color="white" password></u--input>
			</view>
			<view class="regist" style="font-size: 22rpx;">
				<text>没有账号？</text>
				<a style="color:#3c9cff;" @click="goRegist">立即注册</a>
			</view>
			<view class="login pt-4" style="padding-left: 300rpx; ">
				<u-button @click="login" type="primary" size="small" text="登录"
					style="background-image: linear-gradient(to bottom right, rgb(118, 76, 163), rgb(92, 103, 211));"></u-button>
			</view>
		</view>

	</view>
</template>

<script>
	const db = uniCloud.database()
	export default {
		data() {
			return {
				account: "",
				password: "",
				userLocation:{}
			}
		},
		onLoad(e) {
			if (e.account && e.password) {
				this.account = e.account
				this.password = e.password
			}
		},
		methods: {
			goRegist() {
				uni.navigateTo({
					url: "/pages/regist/regist"
				})
			},
			login() {
				if (this.account != "" && this.password != "") {
					db.collection("jiajiao_users").where({
						account: this.account
					}).get().then(res => {
						let data = res.result.data
						if (data.length == 0)
							this.showToast("账号或密码错误")
						else {
							if (this.password == data[0].password) {
								if (data[0].role) {
									this.goNext(data[0].role, data)
								} else {
									
										uni.reLaunch({
											url: `/pages/chose/chose?id=${data[0]._id}`
										})
								}

							} else this.showToast("账号或密码错误")
						}
					})

				} else this.showToast("请输入账号或密码")

			},

			showToast(msg) {
				uni.showToast({
					icon: 'none',
					title: msg
				})
			},
		  async	goNext(identity, data) {
			  await	navigator.geolocation.getCurrentPosition(
				    (position) => {
				      this.userLocation = {
				        latitude: position.coords.latitude,
				        longitude: position.coords.longitude,
				      };
					  db.collection('jiajiao_users').doc(data[0]._id).update({
						  userLocation:this.userLocation
					  })
				    },
				    (error) => {
				      console.error("获取位置失败:", error.message);
				    }
				  );
			   await uni.setStorage({
					key: "jiajiao_demo_account",
					data:data[0],
					success:res=>{
						uni.switchTab({
					url:"/pages/student/student"
				})
					}
				})
				
			}



		}
	}
</script>

<style lang="scss">
	.box {
		background-color: white;
		width: 100%;
		height: 100vh;
		background-image: linear-gradient(to bottom, rgb(114, 135, 254), rgb(130, 88, 186));
	}
</style>